(function (angular) {
    'use strict';
    angular.module('moviecat.movie_list', ['ngRoute', 'movie.services.http'])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider
                .when('/:category/:page?', {
                    controller: 'MovieListController',
                    templateUrl: 'movie_list/view.html'
                })
        }])
        .controller('MovieListController', [
            '$scope',
            '$route',
            '$routeParams',
            'HttpService',
            function ($scope, $route, $routeParams, HttpService) {
                let pageSize = 5;
                $scope.page = parseInt($routeParams.page || 1);
                let start = ($scope.page - 1) * 5;
                $scope.title = 'Loading';
                $scope.movies = {};
                $scope.loading = true;
                $scope.totalCount = 0;//总条数
                $scope.totalPage = 0;//总页数
                HttpService.jsonp('http://api.douban.com/v2//movie/' + $routeParams.category,
                    {
                        start: start,
                        count: 5,
                        q:$routeParams.q
                    }, function (data) {
                        $scope.title = data.title;
                        $scope.movies = data.subjects;
                        $scope.loading = false;
                        $scope.totalCount = data.total;
                        $scope.totalPage = Math.ceil(data.total / pageSize);
                        $scope.$apply();//强制同步数据到界面

                    });
                //暴露一个翻页的行为
                $scope.go = function (page) {
                    if (0 < page && page < $scope.totalPage + 1) {
                        $route.updateParams({page: page});
                    }
                }
            }])
})(angular);
